﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace jnlamp.Web.api
{
    public partial class state_up : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["up"]))
                {
                    string[] s = Request["up"].Split('|');
                    #region 上传数据

                    if ((s.Length -1)%9 == 0)
                    {
                        int t = (s.Length - 1) / 9;

                        for (int i = 0; i < t; i++)
                        {
                            string[] s_t = new string[9];;

                            for (int j = 0; j < 9; j++)
                            {
                                s_t[j] = s[(i*9) + j + 1];
                            }

                            jnlamp.Model.scdInfo scd = getEntity(s_t);
                            jnlamp.BLL.scdInfo scdm = new BLL.scdInfo();

                            if (scd != null)
                            {
                                DataSet ds = scdm.GetListStatus("");
                                if (ds.Tables[0].Rows.Count == 1)
                                {
                                    string status = ds.Tables[0].Rows[0]["status"].ToString();

                                    if (status == "1")
                                    {
                                        scd.gprs_id = s[0];
                                        scdm.Add(scd);
                                    }
                                }

                              
                            }
                            
                        }
                    }
                    #endregion

                    #region 回传数据
                    if ((s.Length - 1) % 9 == 0)
                    {
                        string gprs_id = s[0];

                        jnlamp.BLL.scd_set_opt sm = new BLL.scd_set_opt();
                        jnlamp.BLL.scd_set_time stm = new BLL.scd_set_time();

                        List<jnlamp.Model.scd_set_opt> ls = sm.GetModelList(" gprs_id='" + gprs_id + "' and readcount > 0 ");

                        if (ls.Count > 0)
                        {
                            jnlamp.Model.scd_set_opt s1 = ls[0];

                            s1.readcount = s1.readcount - 1;
                            s1.readtime = System.DateTime.Now;

                            if (sm.Update(s1)) {
                                Response.Write(s1.opt);
                            }
                        }
                        else
                        {
                            List<jnlamp.Model.scd_set_time> lst = stm.GetModelList(" gprs_id='" + gprs_id + "' and readcount > 0 ");

                            if (lst.Count > 0)
                            {
                                jnlamp.Model.scd_set_time st1 = lst[0];

                                st1.readcount = st1.readcount - 1;
                                st1.readtime = DateTime.Now;

                                if (stm.Update(st1))
                                {
                                    string rets = "";

                                    if (st1.ts < 10)
                                    {
                                        rets = "0"+ st1.ts; 
                                    }
                                    else
                                    {
                                        rets = st1.ts.ToString();
                                    }

                                    string re = "TIME" + st1.kdsj + "|" + st1.kdsc + "|" + rets + "|" + st1.vf;

                                    Response.Write(re);
                                }
                            }
                        }

                        
                    }


                    #endregion

                }
            }
        }

        private static jnlamp.Model.scdInfo getEntity(string[] s)
        {
            jnlamp.Model.scdInfo s_1 = new Model.scdInfo();

            s_1.scd_id = s[0];

            int dcb_v_int = Convert.ToInt32(s[1]);

            decimal dcb_v = decimal.Round(Convert.ToDecimal((dcb_v_int * 2.02 / 20020 * 9.2 * 10 + 0.5) / 10), 1);

            s_1.dcb_v = dcb_v;

            int xdc_v_int = Convert.ToInt32(s[2]);

            decimal xdc_v = decimal.Round(Convert.ToDecimal((xdc_v_int * 2.02 / 20020 * 6.1 * 10 + 0.5) / 10), 1);

            s_1.xdc_v = xdc_v;

            int dcb_i_int = Convert.ToInt32(s[3]);

            if (dcb_i_int == 1)
            {
                s_1.dcb_i = true;
            }
            else
            {
                s_1.dcb_i = false;
            }

            int scd_i_int = Convert.ToInt32(s[4]);

            if (scd_i_int == 1)
            {
                s_1.scd_i = true;
            }
            else
            {
                s_1.scd_i = false;
            }

            int dcb_s_int = Convert.ToInt32(s[5]);

            s_1.dcb_s = dcb_s_int;

            int scd_s_int = Convert.ToInt32(s[6]);

            s_1.scd_s = scd_s_int;

            int scd_t_int = Convert.ToInt32(s[7]);

            s_1.scd_t = scd_t_int;

            int vf_int = Convert.ToInt32(s[8]);

            s_1.vf = vf_int;

            s_1.inputTime = System.DateTime.Now;

            int vf_result = (Convert.ToInt32(s[0]) + dcb_v_int + xdc_v_int + dcb_i_int + scd_i_int + dcb_s_int + scd_s_int + scd_t_int)%100;

            if (vf_result == vf_int)
            {
                return s_1;
            }
            else
            {
                return null;
            }

           
        }
    }
}